﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.Net;


namespace Krile.Subsystem
{
    /// <summary>
    /// Checking Update
    /// </summary>
    public static class UpdateChecker
    {
        public static double Check()
        {
            if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
            {
                // Network not available
                return Define.Version;
            }

            var nc = new Krile.Common.NetConnect();
            var ret = nc.WebConnectDownloadString(new Uri(Define.UpdateUri), "GET", null);
            if (!ret.Succeeded)
            {
                if (ret.Exception != null)
                    Debugger.AddReport(ret.Exception);
                else
                    Debugger.AddReport(Lang.Msg.Update_ConnectError + Lang.Msg.Update_ConnectFailed);
                return Define.Version;
            }
            if (ret.Data == null)
            {
                Debugger.AddReport(Lang.Msg.Update_ConnectError + Lang.Msg.Update_DataInvalid);
                return Define.Version;
            }
            double dlVer = 0;
            if (!double.TryParse(ret.Data, out dlVer))
            {
                Debugger.AddReport(Lang.Msg.Update_ConnectError + Lang.Msg.Update_DataInvalid);
                return Define.Version;
            }
            return dlVer;
        }
    }
}
